import { makeAutoObservable, runInAction } from 'mobx'
import Service from '../service'

class knowledgeStore {
    constructor() {
        makeAutoObservable(this)
    }
    data = []
    addInfo = {
        sort: null,
        title: null,
        outline: null,
        user_name:null
    }
    
    //html转字符串
    format() { 
        const str = document.getElementById('EditorBox').innerText;
        this.addInfo.outline=str
        console.log('mobxstr',this.addInfo.outline);
        console.log(typeof this.addInfo.outline);
    }
     //修改数组对象中的某一个键名
    changeKey(arr,key1) { 
        for (let i = 0; i < arr.length; i++) { 
            arr[i].key = arr[i][key1];
        }
    }

    getAllListData({ currentPage, pageSize }) {
        return new Promise((resovle, reject) => {
           Service.KnowledgeService.Knowledge.getAllList({ currentPage, pageSize }).then((data) => {
                resovle(data)
               console.log('请求成功1', data);
                runInAction(() => {
                    this.data = data.data.data;
                    this.changeKey(this.data,'k_id')
                })
            }).catch((err) => {
                reject(err)
            })
        })
    }
    
    getSearchListData({ currentPage, pageSize,title }) {
        return new Promise((resovle, reject) => {
            Service.KnowledgeService.Knowledge.getSearchList({ currentPage, pageSize,title }).then((data) => {
                resovle(data)
                console.log('res请求成功', data.data);
                runInAction(() => {
                    this.data = data.data.data;
                })
            }).catch((err) => {
                reject(err)
            })
        })
    }

    getSaleIndustryListData({ currentPage, pageSize,sort }) {
        return new Promise((resovle, reject) => {
            Service.KnowledgeService.Knowledge.getSaleIndustryList({ currentPage, pageSize,sort }).then((data) => {
                resovle(data)
                console.log('sale请求成功', data.data);
                runInAction(() => {
                    this.data = data.data.data;
                    this.changeKey(this.data,'k_id')
                })
            }).catch((err) => {
                reject(err)
            })
        })
    }

    delListData({ currentPage, pageSize,k_id}) {
        return new Promise((resovle, reject) => {
            Service.KnowledgeService.Knowledge.delList({ currentPage, pageSize, k_id }).then((data) => {
                resovle(data)
                console.log('del请求成功', data.data);
                // runInAction(() => {
                //     this.data = data.data.data;
                // })
            }).catch((err) => {
                reject(err)
            })
        })
    }
    addListData({ currentPage, pageSize,sort,title,outline,user_name}) {
        return new Promise((resovle, reject) => {
            Service.KnowledgeService.Knowledge.addList({ currentPage, pageSize, sort,title,outline,user_name }).then((data) => {
                resovle(data)
                console.log('add请求成功', data.data);
                // runInAction(() => {
                //     this.data = data.data.data;
                // })
            }).catch((err) => {
                reject(err)
            })
        })
    }

}
export default knowledgeStore